//
//  ChangePhoneNumViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/29.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "ChangePhoneNumViewController.h"
#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "CommonView.h"
@implementation BGMTextFile

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        self.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = YES;
        self.myTextFile = [[UITextField alloc]initWithFrame:CGRectMake(15, 0, frame.size.width-30, frame.size.height)];
        self.myTextFile.font = [UIFont systemFontOfSize:17];
        [self addSubview:self.myTextFile];
    }
    return self;
}

@end



@interface ChangePhoneNumViewController ()

@property (nonatomic, strong)BGMTextFile *iPhoneText;
@property (nonatomic, strong)BGMTextFile *codeText;


@property (nonatomic, strong)NSString *str1;


@end

@implementation ChangePhoneNumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+100;
    self.titleBtn.frame = rect;
    [self.titleBtn setTitle:@"更改绑定手机" forState:UIControlStateNormal];
    float y = self.navview.frame.size.height;
    self.iPhoneText = [[BGMTextFile alloc]initWithFrame:CGRectMake(15, y+30, SCREENW-30, 55)];
    self.iPhoneText.myTextFile.placeholder = @"输入新的手机号";
    self.iPhoneText.myTextFile.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:self.iPhoneText];
    
    y = y+self.iPhoneText.frame.size.height+30;
    self.codeText = [[BGMTextFile alloc]initWithFrame:CGRectMake(15, y+15, (SCREENW)/2+30, 55)];
    self.codeText.myTextFile.placeholder = @"输入手机验证码";
    self.codeText.myTextFile.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:self.codeText];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(self.codeText.frame.origin.x+self.codeText.frame.size.width+10, self.codeText.frame.origin.y, SCREENW-self.codeText.frame.size.width-10-30, 55)];
    btn.backgroundColor = bgmyelloeColor;
    btn.layer.cornerRadius = 8;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [btn setTitleColor:bgmmainColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:btn];
    btn.tag = 100;
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    y = y+55+30;

    UIButton *submitbtn = [[UIButton alloc]initWithFrame:CGRectMake(15, y, SCREENW-30, 55)];
    submitbtn.backgroundColor = bgmyelloeColor;
    submitbtn.layer.cornerRadius = 8;
    submitbtn.layer.masksToBounds = YES;
    [submitbtn setTitle:@"确定" forState:UIControlStateNormal];
    [submitbtn setTitleColor:bgmmainColor forState:UIControlStateNormal];
    submitbtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:submitbtn];
    submitbtn.tag = 200;
    [submitbtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)clickBtn:(UIButton *)btn{
    if (btn.tag ==100) { //获取验证码
        [BGMHTTP requestWithMethodName:@"login/getsmsCode" parameters:@{@"phoneNum":self.iPhoneText.myTextFile.text} completion:^(BOOL success, NSError *error, NSDictionary *result) {
            [self getCode:result];
        }];
    }else{  //确定
        BGMUser *user = [BGMUser shareInstance];
        NSDictionary *param = @{@"userToken":user.userToken,@"userId":user.userId,@"newPhoneNum":self.iPhoneText.myTextFile.text,@"smsCode":self.str1};
        
        [BGMHTTP requestWithMethodName:@"users/changePhoneNum" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {
            [self gotoBeForeVC];
        }];
    }
    
}
- (void)gotoBeForeVC{
    [self.navigationController popViewControllerAnimated:YES];
//    [CommonView getCommonAlert:@"" message:@"手机号已更换" ok:@"OK" cancel:@"OK"];
}
- (void)getCode:(NSDictionary *)dictionary{
    dispatch_async(dispatch_get_main_queue(), ^{
         self.str1 = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"data"]];
    });
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
